﻿#region Using Statements
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
#endregion

namespace TheSpirit
{
    public class Animation
    {
        public Dictionary<string, AnimationStrip> animations =
            new Dictionary<string, AnimationStrip>();

        protected bool flipped = false;

        protected string currentAnimation;

        public bool Flipped
        {
            set { flipped = value; }
            get { return flipped; }
        }
        public string CurrentAnimation
        {
            set { currentAnimation = value; }
            get { return currentAnimation; }
        }

        public void AddAnimation(string name, AnimationStrip animationStrip)
        {
            animations.Add(name, animationStrip);
        }

        public void PlayAnimation(string name)
        {
            if (!(name == null) && animations.ContainsKey(name))
            {
                currentAnimation = name;
                animations[name].Play();
            }
        }

        public void updateAnimation(GameTime gameTime)
        {
            //Key를 갖고 잇어야함. 즉 애니메이션이 존재하면,
            if (animations.ContainsKey(currentAnimation))
            {
                //애니메이션이 끝났다면
                if (animations[currentAnimation].FinishedPlaying)
                {
                    //애니메이션은 다음 애니메이션을 가르키게 한다.
                    PlayAnimation(animations[currentAnimation].NextAnimation);
                }
                else
                {
                    //애니메이션이 안끝낫다면
                    animations[currentAnimation].Update(gameTime);
                }
            }
        }

        public void DrawAnimation(SpriteBatch spriteBatch, Rectangle position)
        {
            if (animations.ContainsKey(currentAnimation))
            {
                SpriteEffects effect = SpriteEffects.None;

                //flip 시킬 것인가?
                if (flipped)
                {
                    effect = SpriteEffects.FlipHorizontally;
                }

                spriteBatch.Draw(
                animations[currentAnimation].Texture,
                position,
                animations[currentAnimation].FrameRectangle,
                Color.White, 0.0f, Vector2.Zero, effect, 0f);
                //animation이 끝났을 경우
                if (animations[currentAnimation].FinishedPlaying == true)
                {
                    PlayAnimation("normal");
                }
            }
        }
    }
}
